package tests;

import static org.junit.Assert.fail;
import static util.Constant.STR_EXCEPTION_SHOULD_HAVE_OCCURED;
import static util.Constant.STR_INVALID_ID;
import static util.Constant.STR_ITEM_ALREADY_EXIST;
import static util.Constant.STR_ITEM_NOT_EXIST;

import java.util.HashSet;

import junit.framework.Assert;

import org.junit.Test;

import base.Itemlist;

public class ItemlistTest {

	@Test
	public void testItemlist() {
		try {
			new Itemlist(-1);
			fail(STR_EXCEPTION_SHOULD_HAVE_OCCURED);
		} catch (Exception e) {
			Assert.assertEquals(STR_INVALID_ID, e.getMessage());
		}

		try {
			new Itemlist(-4, "toto", new HashSet<Integer>());
			fail(STR_EXCEPTION_SHOULD_HAVE_OCCURED);
		} catch (Exception e) {
			Assert.assertEquals(STR_INVALID_ID, e.getMessage());
		}
	}

	@Test
	public void testAdd() {
		Itemlist itemList = new Itemlist(1, "toto", new HashSet<Integer>());
		try {
			itemList.add(-1);
			fail(STR_EXCEPTION_SHOULD_HAVE_OCCURED);
		} catch (Exception e) {
			Assert.assertEquals(STR_INVALID_ID, e.getMessage());
		}

		try {
			Integer itemID = new Integer(5);
			itemList.add(itemID);
			itemList.add(itemID);
			fail(STR_EXCEPTION_SHOULD_HAVE_OCCURED);
		} catch (Exception e) {
			Assert.assertEquals(STR_ITEM_ALREADY_EXIST, e.getMessage());
		}
	}

	@Test
	public void testRemove() {
		Itemlist itemList = new Itemlist(1, "toto", new HashSet<Integer>());
		try {
			itemList.remove(-1);
			fail(STR_EXCEPTION_SHOULD_HAVE_OCCURED);
		} catch (Exception e) {
			Assert.assertEquals(STR_INVALID_ID, e.getMessage());
		}

		try {
			itemList.remove(5);
			fail(STR_EXCEPTION_SHOULD_HAVE_OCCURED);
		} catch (Exception e) {
			Assert.assertEquals(STR_ITEM_NOT_EXIST, e.getMessage());
		}
	}

	@Test
	public void testContains() {
		Itemlist itemList = new Itemlist(1, "toto", new HashSet<Integer>());

		try {
			Integer itemID = new Integer(5);
			itemList.add(itemID);
			Assert.assertTrue(itemList.contains(itemID));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Test
	public void testEquals() {
		Itemlist itemList1 = new Itemlist(1, "toto", new HashSet<Integer>());
		Assert.assertFalse(itemList1.equals(new String()));

		Itemlist itemList2 = new Itemlist(1, "tata", new HashSet<Integer>());
		Assert.assertTrue(itemList1.equals(itemList2));

		Itemlist itemList3 = new Itemlist(4, "tata", new HashSet<Integer>());
		Assert.assertFalse(itemList1.equals(itemList3));

	}
}
